<?php

class Jd_task_jd_list_pool_model extends MY_Model
{


    public function __construct()
    {
        $this->modelename = 'Jd_task_jd_list_model';
        $this->tablename = 'task_jd_list_pool';
        parent::__construct();
    }


    /**
     *
     * 将任务子单加入任务池
     * @param array $array
     * @return INT
     */
    public function addtasklisttopoll($array = array())
    {
        try {
            return @$r = $this->add($this->tablename, $array);
        } catch (Exception $e) {
            return false;
        }


    }


    /**
     * 刷手从任务池获取任务
     */
    public function HunterGetTask($epage, $Exclude = "", $type = 1, $shop_name = "")
    {
        $team = $this->userinfo['team'];
        $team = intval($team);
        $sql = "select * from jd_task_jd_list_pool  where   `type`=$type  AND  `status` =0  AND `hunter_id`=0  AND `shop_name` <>'' AND (`team` is null or locate($team,`team`)   or `team`=0) group by `shop_name` limit 0," . $epage;

        $sql = "select * from jd_task_jd_list_pool  where   `type`=$type  AND  `status` =0  AND `hunter_id`=0  AND `shop_name` <>'' AND (`team` is null or  `team`=$team or `team`=0) group by `shop_name` limit 0," . $epage;


        if (!empty($Exclude)) {//重新获取时排除之前的商铺
            foreach ($Exclude as $id => $v) {
                $str[] = " AND  `shop_name`<>  '$v'";
            }
            $str = implode($str, " ");
            $sql = "select * from jd_task_jd_list_pool  where `type`=$type  AND   `status` =0  AND `hunter_id`=0  AND `shop_name`<>''  $str  AND(`team` is null or  `team`=$team or `team`=0)   or `team`=0)  group by `shop_name` limit 0," . $epage;
        }
        if ($shop_name) {
            $sql = "select * from jd_task_jd_list_pool  where   `type`=$type  AND  `status` =0  AND `hunter_id`=0  AND `shop_name`='$shop_name'    limit 0,1";
        }


        $result = $this->GetBySql($sql);
        $row['list'] = $result;
        $row['count'] = count($result);
        return $row;
    }


    public function HunterGetTaskTurn($epage, $turn = 1, $user_id)
    {
        $team = $this->userinfo['team'];
        $team = intval($team);

        $hasend = array();
        if ($turn > 1) {
            $sql = "select `shop_name` from jd_task_jd_list_pool  where   `type`=2  AND  `status` >0  AND `hunter_id`=$user_id AND turn= $turn  group by  `shop_name` limit 0,100";
            $endshop = $this->GetBySql($sql);
            if ($endshop) {
                foreach ($endshop as $id => $v) {
                    $hasend[] = $v['shop_name'];
                }
            }
        }
        if ($hasend) {
            $sql = "select `shop_name`,count(1) as `num` from jd_task_jd_list_pool  where  
 `type`=2  AND  `status` =0  AND `hunter_id`=0 AND `shop_name` <>'' AND `shop_name` NOT IN (" . implode(",", $hasend) . ")  
  AND (`team` is null or  `team`=$team or `team`=0)  AND turn=0 group by `shop_name`  order by `num` desc  limit 0,1";
        } else {
            $sql = "select `shop_name`,count(1) as `num` from jd_task_jd_list_pool  where  
 `type`=2  AND  `status` =0  AND `hunter_id`=0 AND `shop_name` <>'' AND `shop_name` AND (`team` is null or  `team`=$team or `team`=0)  AND turn=0 group by `shop_name`  order by `num` desc  limit 0,1";
        }

        $one = $this->GetBySql($sql, 0);
        if (empty($one)) {
            $row['list'] = 0;
            return $row;
        }
        $shop_name = $one['shop_name'];
        $sql = "select * from jd_task_jd_list_pool  where  `shop_name`= '$shop_name'  limit 0," . $epage;


        $result = $this->GetBySql($sql);
        $row['list'] = $result;
        $row['count'] = count($result);
        return $row;
    }


    /**
     *
     * 刷手从任务池中更新标记该任务为自己的任务
     * @param $user_id
     * @param array $array
     */
    public function HunterAccept($user_id, $array = array(), $awz, $trun = 0)
    {
        if (empty($array)) {
            return false;
        }
        if (count($array) > 20) {
            return false;
        }
        $sql = "update  jd_" . "$this->tablename set `status`=1,hunter_id=$user_id ,awz='$awz' where  `status`=0 AND  id in  (" . implode($array, ",") . ") LIMIT 20";

        if ($trun) {
            $sql = "update  jd_" . "$this->tablename set `status`=1,hunter_id=$user_id ,awz='$awz',turn=$trun where  `status`=0 AND  id in  (" . implode($array, ",") . ") LIMIT 20";
        }

        $r = $this->db->query($sql);
        return $r;
    }


    /**
     * 刷手放弃当前所有已接任务
     * @param $user_id
     * @return mixed
     */
    public function HunterUnaccept($user_id)
    {
        $sql = "update  jd_" . "$this->tablename set `status`=0,hunter_id=0 ,awz='' where  `status`=1 AND hunter_id= $user_id ";
        $r = $this->db->query($sql);
        return $r;
    }


    /**
     * 刷手完成任务
     */
    public function finishtask($user_id, $array = array())
    {
        $real_price = $array['real_price'];
        $id = $array['id'];
        $orderid = $array['orderid'];
        $awz = isset($array['awz']) ? $array['awz'] : "";

        $result = $this->db->select('listid,status,hunter_id,awz')->from('jd_task_jd_list_pool')->where("id=" . $id)->get()->result_array();
        if ($result[0]['status'] == 2 || $result[0]['hunter_id'] != $this->userId) {
            return false;
        }
        $addrees = $array['addrees'];

        $sql = "update  jd_" . "$this->tablename set `status`=2,hunter_id=$user_id, `real_price`=$real_price ,
         `orderid`='$orderid' ,`addrees`='$addrees'  where id =$id";
        $r = $this->db->query($sql);
        /* if($this->userId==415){

           echo $sql;exit;
         }*/

        if (!$r) {
            return false;
        }
        $id = $result[0]['listid'];
        $finishtime = time();
        $team = $array['team'];
        $huntername = $array['username'];
        if (empty($awz)) {
            $awz = $result[0]['awz'];
        }
        $sql = "update  jd_task_jd_list set `status`=2,hunter_id=$user_id, `real_price`=$real_price ,`awz`='$awz', 
 `orderid`='$orderid' ,`finishtime`=$finishtime,`team`='$team',hunter_name='$huntername', `addrees`='$addrees' where id =$id";
        $r = $this->db->query($sql);
        return $r;
    }


    /**
     * 刷手放弃任务 任务重新可以被其他人获取
     */
    public function replacetask($idstr)
    {
        $user_id = $this->userId;

        if (is_array($idstr)) {
            $idstr = implode($idstr, ",");
            $sql = "update  jd_task_jd_list_pool set `status`=0 , hunter_id=0, awz='' where hunter_id=$user_id  AND id  in (" . $idstr . ") AND status<2";
        } else {
            $sql = "update  jd_task_jd_list_pool set `status`=0 , hunter_id=0, awz='' where hunter_id=$user_id  AND id =$idstr AND status<2";
        }
        $r = $this->db->query($sql);
        return $r;
    }


    public function truncatelog()
    {
        $sql = "truncate  jd_task_jd_list_pool ";

        //$sql = "delete  from jd_task_jd_list_pool where `status`=2";
        $r = $this->db->query($sql);
    }

    public function taskupdate($row)
    {
        $table = $this->tablename;
        $primary = "taskid";
        $id = $row['id'];
        unset($row['id']);
        return $this->update($table, $primary, $id, $row, 10000); // TODO: Change the autogenerated stub
    }

    /**
     * 重置任务状态为初始
     * @param $id
     * @return mixed
     */
    public function ResetStatus($id)
    {
        $sql = "update  jd_task_jd_list_pool  set `status`= 0  ,hunter_id=0,awz='' where taskid =$id  AND hunter_id>0 AND  `status`=1 ";
        return $r = $this->db->query($sql);
    }

    /**
     * 废弃未被接取任务
     * @param $id
     * @return mixed
     */
    public function CancelStatus($id)
    {
        $sql = "update jd_task_jd_list_pool  set `status`= 3  ,hunter_id=0 where taskid =$id  AND hunter_id=0 AND  `status`=0 ";
        return $r = $this->db->query($sql);
    }


    public function getpoolsnumber()
    {
        $sql = "select  `type`, count(1) as 'num' from jd_task_jd_list_pool  where   `status` <2 AND    `type`>0  group by `type` ";
        return $result = $this->GetBySql($sql);
    }


    public function checkprice()
    {

    }


}
